package com.qf.controller;

import com.qf.annotation.MyLog;
import com.qf.entity.SysUser;
import com.qf.service.SysUserService;
import com.qf.util.JwtUtils;
import com.qf.util.R;
import com.qf.util.RedisUtil;
import com.qf.util.RsaUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.UUID;

/**
 * @ClassName: SysUserController
 * @Author: WangHao
 * @Date: 2025/4/21 15:26
 * @Description:
 * @Version: 1.0
 */
@RestController
@CrossOrigin
@Slf4j
public class SysUserController {

    @Autowired
    private SysUserService service;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private RedisUtil redisUtil;

    private HashMap<String, Object> map = new HashMap<>();

    /**
     * 登录，返回token
     */
    // @PostMapping("/user/login")
    // @MyLog(value = "登录", type = "登录")
    // public R login(@RequestBody SysUser sysUser, HttpSession session) throws Exception {
    //     // sysUser.setPassword(RsaUtils.decryptByPrivateKey(sysUser.getPassword()));
    //     SysUser sysUser1 = service.login(sysUser);
    //     if(sysUser1 == null){
    //         return R.fail();
    //     }
    //     String token = UUID.randomUUID().toString();
    //     map.put("token", token);
    //     map.put(token,sysUser1);
    //     session.setAttribute("sysUser", sysUser1);
    //     return R.ok(token);
    // }

    /**
     * 二次请求，使用token查询用户信息
     */
    // @GetMapping("/user/info")
    // public R getInfo(String token){
    //     SysUser sysUser = (SysUser) map.get(token);
    //     if (sysUser != null) {
    //         return R.ok(sysUser);
    //     }
    //     return R.ok();
    // }

    /**
     * 系统用户登录
     */
    @PostMapping("/user/login")
    @MyLog (value = "用户登录", type = "登录")
    public R login(@RequestBody SysUser sysUser, HttpSession session) throws Exception {
        // 根据用户名+密码查数据库,将信息存储到redis,查到返回token
        // String pwd = RsaUtils.decryptByPrivateKey(sysUser.getPassword( ));

        // 根据用户名+密码查数据库,将信息存储到redis,查到返回token
        log.debug("解密前密码: " + sysUser.getPassword( ));
        String pwd = RsaUtils.decryptByPrivateKey(sysUser.getPassword( ));
        log.debug("解密后密码: " + pwd);
        // 对密码按照注册时规则加密
        // pwd = PwdEncrypt.hashPassword(pwd);
        sysUser.setPassword(pwd);

        String token = service.login(sysUser);
        return R.ok(token);
    }

    /**
     * 根据token获取用户信息
     */
    @GetMapping("/user/info")
    @MyLog(value = "获取用户信息", type = "查询")
    public R getUserInfo(String token) {
        // 根据token从redis中获取用户信息
        Authentication authentication = SecurityContextHolder.getContext( ).getAuthentication( );
        String name = authentication.getName( );
        SysUser sysUser = new SysUser( );
        sysUser.setUsername(name);
        return R.ok(sysUser);
    }


    /**
     * 退出登录
     */
    @GetMapping("/user/logout")
    @MyLog(value = "退出", type = "退出操作")
    public R logout(String token, HttpServletRequest request){
        Claims claims = jwtUtils.parseJWT(token);
        Object uuid = claims.get("uuid");
        redisUtil.deleteObject("user:key:"+uuid);
        return R.ok( );
    }

}
